<?php
namespace App\Http\Controllers\Admin;
use App\http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
class NavController extends BaseController{
    //订单列表页面
    public function nav_list(){
        $islogin = $this->islogin();
        if (!$islogin) {
            return redirect("/admin/login");
        }
        $nav = DB::table("nav")->where("parent_id","0")->get();
        foreach($nav as $key => $value){
            $temp = DB::table("nav")->where("parent_id",$value->nav_id)->get();
            $nav[$key]->son = $temp;
        }
        // dd($nav[1]->son[0]);die;
        return view('admin/nav/nav_list',["nav"=>$nav]);
    }
    public function add_nav(Request $request){
        $islogin = $this->islogin();
        if (!$islogin) {
            return redirect("/admin/login");
        }
        $parent_id = $request->parent_id;
        return view('admin/nav/nav_add',["parent_id"=>$parent_id]);
    }
    public function edit_nav(Request $request){
        $islogin = $this->islogin();
        if (!$islogin) {
            return redirect("/admin/login");
        }
        $nav_id = $request->nav_id;
        $nav = DB::table("nav")->where("nav_id",$nav_id)->first();
        return view('admin/nav/nav_edit',["nav"=>$nav]);
    }
    public function todo_addnav(Request $request){
        $islogin = $this->islogin();
        if (!$islogin) {
            return redirect("/admin/login");
        }
        $data = array(
            "nav_name"=>$request->nav_name,
            "nav_url"=>$request->nav_url,
            "nav_ord"=>$request->nav_ord,
            "parent_id"=>$request->parent_id,
        );
        $res = DB::table("nav")->insert($data);
        if ($res) {
            echo json_encode(array(
                "code"=>"1",
                "msg"=>"添加成功"
            ));
        }else{
            echo json_encode(array(
                "code"=>"2",
                "msg"=>"数据执行有误"
            ));
        }
    }
    public function todo_delnav(Request $request){
        $islogin = $this->islogin();
        if (!$islogin) {
            return redirect("/admin/login");
        }
       $nav_id = $request->nav_id;
       $nav = DB::table("nav")->where("nav_id",$nav_id)->first();
       if($nav->parent_id==0){
            DB::table("nav")->where("parent_id",$nav->nav_id)->delete();
       }
       $res = DB::table("nav")->where("nav_id",$nav_id)->delete();
       if($res){
            echo json_encode(array(
                "code"=>"1",
                "msg"=>"删除成功"
            ));
       }else{
            echo json_encode(array(
                "code"=>"2",
                "msg"=>"数据执行有误"
            ));
       }
    }
    public function todo_editnav(Request $request){
        $islogin = $this->islogin();
        if (!$islogin) {
            return redirect("/admin/login");
        }
        $data = array(
            "nav_name"=>$request->nav_name,
            "nav_url"=>$request->nav_url,
            "nav_ord"=>$request->nav_ord,
            "rand"=>mt_rand(0,99999)
        );
        $res = DB::table("nav")->where("nav_id",$request->nav_id)->update($data);
        if ($res) {
            echo json_encode(array(
                "code"=>"1",
                "msg"=>"保存成功"
            ));
        }else{
            echo json_encode(array(
                "code"=>"2",
                "msg"=>"数据执行有误"
            ));
        }
    }
    //修改排序
    public function changeord(Request $request){
        $islogin = $this->islogin();
        if (!$islogin) {
            return redirect("/admin/login");
        }
        $nav_id = $request->nav_id;
        $nav_ord = $request->nav_ord;
        $data = array(
            "nav_ord"=>$nav_ord
        );
        $res = DB::table("nav")->where("nav_id",$nav_id)->update($data);
        if ($res) {
            echo json_encode(array(
                "code"=>"1",
                "msg"=>"保存成功"
            ));
        }else{
            echo json_encode(array(
                "code"=>"2",
                "msg"=>"数据执行有误"
            ));
        }
    }
} 